package day06;

import jakarta.servlet.Filter;
import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletContextAttributeListener;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebFilter;
import jakarta.servlet.http.HttpFilter;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

//@WebFilter("/*")
public class LogFilter extends HttpFilter {

    @Override
    protected void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException, ServletException {
        try {
            System.out.println(request.getRemoteAddr()+"在"+new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(new Date())+"发送"+request.getRequestURI()+"请求");

            //执行下一个组件
            chain.doFilter(request,response);

            System.out.println(request.getRemoteAddr()+"在"+new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(new Date())+"执行完"+request.getRequestURI()+"请求");

        } catch (Exception e) {
            System.out.println(request.getRemoteAddr()+"在"+new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(new Date())+"执行"+request.getRequestURI()+"请求出错");
            throw e;
        }

    }
}
